<?php
use common\modules\user\models\User;
use yii\db\Migration;

class m150912_123000_user_tables extends Migration
{
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        
        $this->createTable('{{%user_group}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer(),
            'type' => $this->string(20)
                ->notNull(),
            'name' => $this->string(32)
                ->notNull(),
            'description' => $this->text(),
            'group_rank' => $this->string(20),
            'created_at' => $this->integer()
        ], $tableOptions);
        
        $this->createTable('{{%user}}', [
            'id' => $this->primaryKey(),
            'username' => $this->string(32),
            'email' => $this->string()
                ->notNull(),
            'mobile' => $this->string(15),
            'auth_key' => $this->string(32)
                ->notNull(),
            'access_token' => $this->string(32)
                ->notNull(),
            'password_hash' => $this->string()
                ->notNull(),
            'password_reset_token' => $this->string(),
            'source' => $this->smallInteger(2),
            'status' => $this->smallInteger()
                ->notNull()
                ->defaultValue(User::STATUS_ACTIVE),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer(),
            'logged_at' => $this->integer()
        ], $tableOptions);
        
        $this->createTable('{{%user_profile}}', [
            'user_id' => $this->primaryKey(),
            'group_id' => $this->integer(),
            'realname' => $this->string(20),
            'nickname' => $this->string(20),
            'gender' => $this->smallInteger(1),
            'avatar_path' => $this->string(),
            'avatar_url' => $this->string(),
            'fans_count' => $this->integer()->defaultValue(0), // 粉丝数
            'follow_count' => $this->integer()->defaultValue(0), // 关注数
            'favorite_count' => $this->integer()->defaultValue(0), // 收藏数
            'visited_count' => $this->integer()->defaultValue(0), // 被访问次数
            'locale' => $this->string(32)
                ->notNull()
        ], $tableOptions);
        
        $this->createTable('{{%user_auth_client}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()
                ->notNull(),
            'provider' => $this->string(20)
                ->notNull(),
            'provider_user_id' => $this->string(50)
                ->notNull(),
            'provider_access_token' => $this->string(50)
                ->notNull(),
            'provider_refresh_token' => $this->string(50)
                ->notNull(),
            'provider_attributes' => $this->text(),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
        
        $this->addForeignKey('fk_user_group_parent', '{{%user_group}}', 'parent_id', '{{%user_group}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_user', '{{%user_profile}}', 'user_id', '{{%user}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_user_group', '{{%user_profile}}', 'group_id', '{{%user_group}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_user_auth_client_user', '{{%user_auth_client}}', 'user_id', '{{%user}}', 'id', 'cascade', 'cascade');
    }

    public function safeDown()
    {
        $this->dropForeignKey('fk_user_auth_client_user', '{{%user_auth_client}}');
        $this->dropForeignKey('fk_user_group', '{{%user_profile}}');
        $this->dropForeignKey('fk_user', '{{%user_profile}}');
        $this->dropForeignKey('fk_user_group_parent', '{{%user_group}}');
        
        $this->dropTable('{{%user_auth_client}}');
        $this->dropTable('{{%user_profile}}');
        $this->dropTable('{{%user}}');
        $this->dropTable('{{%user_group}}');
    }
}
